import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM, RELEASE_TYPE_ENUM } from "../base/EnumIndex";
import { LEVEL_TYPE, LevelManager } from "../manager/LevelManager";
import { UIManager } from "../manager/UIManager";
import { Tools } from "../common/Tools";
import { AdManager } from "../ads/AdManager";
import { AudioManager } from "../manager/AudioManager";
import { EventManager } from "../manager/EventManager";
import { ResManager } from "../manager/ResManager";
import TouchScale from "../item/Common/TouchScale";
import { TimeTools } from "../common/TimeTools";

const { ccclass, property } = cc._decorator;

@ccclass
export default class HomeUI extends cc.Component {

    /** 当前金钱 */
    gold = 0;

    clickNum = 0;

    autoNum = 0

    /** 自动点击 */
    autoClick = false;

    static overLevel = 0;

    private isSign = false;

    @property(cc.Node)
    jiajuLvUpNode: cc.Node = null;


    @property(cc.Node)
    baofuzhi: cc.Node = null;

    /** 饱腹值数值 */
    baofuzhiNum = 0;


    @property(cc.Label)
    goldStr: cc.Label = null;


    /** 升级特效 */
    @property(cc.Node)
    jiajuLvUptexiao: cc.Node = null;


    /** 风 */
    @property(cc.Node)
    feng: cc.Node = null;


    /** 手指 */
    @property(cc.Node)
    finger: cc.Node = null;


    @property(cc.Label)
    jiEzhiNum: cc.Label = null;
    /** 入口有奖 */
    @property(cc.Node)
    entryBtn: cc.Node = null;
    /** 添加桌面 */
    @property(cc.Node)
    addBtn: cc.Node = null;

    async onLoad() {

        // AudioManager.playBG("startBgm", 1);
        AudioManager.stopBG();
        this.initJiaju();
        this.initBaofu();
        this.initGold();
        this.clickZhuangxiuBtn();

        let men = Number(Tools.getStorage("门"));
        let chuanghu = Number(Tools.getStorage("窗户"));
        if (!chuanghu || !men) {
            let audioId = await AudioManager.playAudio("风声音效", 1, BUNDLE_TYPE_ENUM.HOME)
            AudioManager.setAudioLoop(audioId, true)
        }

        if (this.baofuzhiNum <= 30) {
            AudioManager.playAudio("肚子好饿", 1, BUNDLE_TYPE_ENUM.HOME)
        }

        if (cc.sys.os == cc.sys.OS_WINDOWS) {
            this.node.getChildByName("beijing").on(cc.Node.EventType.MOUSE_WHEEL, (touches: cc.Event.EventMouse) => {
                this.node.getChildByName("beijing").getComponent(TouchScale).mouseWheelCall(touches);
            }, this);
        }


        this.node.getChildByName("beijing").on(cc.Node.EventType.TOUCH_MOVE, (touchs: cc.Event.EventTouch) => {
            this.node.getChildByName("beijing").getComponent(TouchScale).touchMoveCall(touchs);

        }, this);
        this.node.getChildByName("beijing").on(cc.Node.EventType.TOUCH_END, (touchs: cc.Event.EventTouch) => {
            this.node.getChildByName("beijing").getComponent(TouchScale).touchMoveEnd(touchs);

        }, this);
    }
    onDestroy() {
        EventManager.ins.off(EVENT_NAME_ENUM.UPDATE_HOME_COST, this.updateHomeCost, this);
        AudioManager.stopAudioForName("风声音效")
    }
    start() {

        EventManager.ins.on(EVENT_NAME_ENUM.UPDATE_HOME_COST, this.updateHomeCost, this);
        this.checkIsSign();
        if (!this.isSign || !AdManager.isFuFangClaim) {
            if (!this.isSign) {
                UIManager.ins.showPop('signUI2', BUNDLE_TYPE_ENUM.HOME);
            } else if (!AdManager.isFuFangClaim && AdManager.releaseType === RELEASE_TYPE_ENUM.bytedance) {
                UIManager.ins.showPop('douyinPropUI', BUNDLE_TYPE_ENUM.HOME);
            }
        }

        
        AdManager.isShowAddShortcutBtn((active: boolean) => {
            this.addBtn.active = active;
        });
        AdManager.isShowGiftBtn((active: boolean) => {
            console.log("是否开启入口按钮", active);
            this.entryBtn.active = active;
        });
    }

    protected update(dt: number): void {

    }

    updateHomeCost() {
        this.initBaofu();
        this.initGold();
    }

    /** 初始化饱腹值 */
    initBaofu() {
        this.baofuzhiNum = Number(Tools.getStorage("饱腹值"));
        this.baofuzhi.getComponent(cc.ProgressBar).progress = this.baofuzhiNum / 100;

        // if (this.baofuzhiNum > 70) {
        //     this.baofuzhi.children[1].active = true
        //     this.baofuzhi.children[2].active = false
        //     this.baofuzhi.children[3].active = false
        // } else if (this.baofuzhiNum > 30) {
        //     this.baofuzhi.children[1].active = false
        //     this.baofuzhi.children[2].active = true
        //     this.baofuzhi.children[3].active = false
        // } else {
        //     this.baofuzhi.children[1].active = false
        //     this.baofuzhi.children[2].active = false
        //     this.baofuzhi.children[3].active = true
        // }

        if (this.baofuzhiNum >= 100) {
            AudioManager.playAudio("我们吃饱啦", 1, BUNDLE_TYPE_ENUM.HOME)
        }

        this.jiEzhiNum.string = this.baofuzhiNum + "%"
    }

    /** 初始化金钱 */
    initGold() {
        let gold = parseFloat(Tools.getStorage("gold", true)) ? parseFloat(Tools.getStorage("gold", true)) : 0;
        this.goldStr.string = "" + gold;
    }



    initJiaju() {
        for (const key in HomeUI.shengjidian) {
            let jieduan = Number(Tools.getStorage(key));
            for (let index = 0; index < this.node.getChildByName("beijing").getChildByName("qiantao").getChildByName("升级内容").getChildByName(key).childrenCount; index++) {
                const element = this.node.getChildByName("beijing").getChildByName("qiantao").getChildByName("升级内容").getChildByName(key).children[index];
                if (index == jieduan) {
                    element.active = true;
                    if (element.name == "破门") {
                        this.feng.active = true;
                    } else if (element.name == "好门") {
                        this.feng.active = false;
                    }
                } else {
                    element.active = false;
                }
            }
        }

        let men = Number(Tools.getStorage("门"));
        let chuanghu = Number(Tools.getStorage("窗户"));
        if (chuanghu && men) {
            AudioManager.stopAudioForName("风声音效")
        }
    }

    jiajuLvUp(event, name) {
        AudioManager.playClick();
        let jieduan = Number(Tools.getStorage(name));

        let nextJieDuan = HomeUI.shengjidian[name][jieduan + 1];
        if (nextJieDuan) {
            for (const key in nextJieDuan) {
                const element = nextJieDuan[key];
                // 获取通关数
                let story = Tools.getStorage('PassLv_1', true);
                if (!story) {
                    story = [];
                }
                if (story.length >= element) {
                    Tools.setStorage(name, jieduan + 1);


                    AudioManager.playAudio("装修音效", 1, BUNDLE_TYPE_ENUM.HOME);
                    this.jiajuLvUptexiao.getChildByName(name).getChildByName("shengji").active = true;
                    this.jiajuLvUptexiao.getChildByName(name).getChildByName("shengji").getComponent(dragonBones.ArmatureDisplay).playAnimation("newAnimation", 1);
                } else {
                    UIManager.ins.showPop('changjingLvUpUI', BUNDLE_TYPE_ENUM.HOME, { name: key, num: element - story.length });
                }
            }
        } else {
            UIManager.ins.showToast("当前已到最高级");
        }
        this.initJiaju();
        this.clickZhuangxiuBtn();
    }

    CloseZhuangXiu() {
        this.jiajuLvUpNode.active = false;
    }

    clickLevelClose() {
        AudioManager.playClick();
        UIManager.ins.close();
        // LevelManager.ins.selectLevelType = LEVEL_TYPE.ALL_TYPE;
        // UIManager.ins.showUI("LevelUI", BUNDLE_TYPE_ENUM.HOME);
        // UIManager.ins.showUI("startUI", BUNDLE_TYPE_ENUM.HOME);
        UIManager.ins.showUI("partnerUI", BUNDLE_TYPE_ENUM.HOME);
    }

    clickShop() {
        AudioManager.playClick();
        UIManager.ins.showPop("ShopUI", BUNDLE_TYPE_ENUM.HOME);
    }

    clickBtnGift() {
        AudioManager.playClick();

        UIManager.ins.showPop('signUI2', BUNDLE_TYPE_ENUM.HOME);
    }

    static goldUpData(gold) {
        Tools.setStorage("gold", gold);
        let ReturnGold = parseFloat(Tools.getStorage("gold", true)) ? parseFloat(Tools.getStorage("gold", true)) : 0;
        return ReturnGold
    }

    checkIsSign() {
        let data: any[] = Tools.getStorage("signData2", true);
        if (data) {
            let lastTime = data[data.length - 1];
            let day1 = TimeTools._ins.getTimeData(lastTime).day;
            let day2 = TimeTools._ins.getTimeData().day;
            if (day1 == day2) {
                console.log("今日已签到");
                this.isSign = true;
                return;
            }
        }
    }

    clickZhuangxiuBtn() {
        this.jiajuLvUpNode.active = true;

        let fingerStatus = true;

        for (const key in this.jiajuLvUpNode.children) {
            const element = this.jiajuLvUpNode.children[key];

            for (const key2 in HomeUI.shengjidian) {
                const element2 = HomeUI.shengjidian[key2];
                if (element.name == key2) {
                    let jieduan = Number(Tools.getStorage(element.name));
                    let nextJieDuan = HomeUI.shengjidian[element.name][jieduan + 1];
                    if (nextJieDuan) {
                        element.active = true;
                        if (this.ifjiajuLvUp(element.name)) {
                            element.children[0].active = true;
                        } else {
                            element.children[0].active = false;
                        }

                        if (fingerStatus) {
                            fingerStatus = false
                            this.finger.parent = element;

                            if (this.ifjiajuLvUp(element.name)) {
                                this.finger.children[0].active = false;
                                element.getComponent(cc.Sprite).enabled = true;
                            } else {
                                this.finger.children[0].active = true;
                                element.getComponent(cc.Sprite).enabled = false;
                            }
                        }
                    } else {
                        element.active = false;
                    }
                }
            }
        }
        if (fingerStatus) {
            this.finger.active = false;
        } else {
            this.finger.active = true;
        }
    }

    ifjiajuLvUp(name) {
        let keshengji = false;
        let jieduan = Number(Tools.getStorage(name));
        let nextJieDuan = HomeUI.shengjidian[name][jieduan + 1];
        if (nextJieDuan) {
            for (const key in nextJieDuan) {
                const element = nextJieDuan[key];
                let story = Tools.getStorage('PassLv_1', true);
                if (!story) {
                    story = [];
                }
                if (story.length >= element) {
                    keshengji = true;
                } else {
                    keshengji = false;
                }
            }
        } else {
            keshengji = false
        }
        return keshengji;
    }

    /** 升级解锁 */
    static jiajuLockoff() {
        let shengji = false
        let jiangLi = '';
        for (const key in HomeUI.shengjidian) {
            const element = HomeUI.shengjidian[key];

            let jieduan = Number(Tools.getStorage(key));

            let nextJieDuan = HomeUI.shengjidian[key][jieduan + 1];
            if (nextJieDuan) {
                for (const key2 in nextJieDuan) {
                    const element2 = nextJieDuan[key2];
                    // 获取通关数
                    let story = Tools.getStorage('PassLv_1', true);
                    if (!story) {
                        story = [];
                    }
                    if (story.length == element2) {
                        shengji = true;
                        jiangLi = key2
                        break;
                    }
                }
                if (shengji) {
                    break;
                }
            }
        }
        return { "shengji": shengji, "jiangLi": jiangLi }
    }

    clickBtnEntry() {
        AudioManager.playClick();
        UIManager.ins.showPop('douyinPropUI', BUNDLE_TYPE_ENUM.HOME);
    }
    clickBtnAddShortCut() {
        AudioManager.playClick();
        // AdManager.addTable(() => {
        //     this.addBtn.node.active = false;
        // });
        // UIManager.ins.showPop('addShortcutUI', BUNDLE_TYPE_ENUM.HOME);
        AdManager.addTable(() => EventManager.ins.emit(EVENT_NAME_ENUM.HIDE_ADD_SHORTCUT_BTN));
    }

    
    clickBtnStart() {
        AudioManager.playClick();
        UIManager.ins.close();
        LevelManager.ins.selectLevelType = LEVEL_TYPE.ALL_TYPE;
        UIManager.ins.showUI("LevelUI", BUNDLE_TYPE_ENUM.HOME);
    }

    protected onDisable(): void {

    }


    // static shengjidian =
    //     {
    //         "墙": [
    //             { "墙壁": 0 },
    //             { "墙壁": 5 },
    //         ],
    //         "地板": [
    //             { "地板": 0 },
    //             { "地板": 6 },
    //         ],
    //         "灯": [
    //             { "煤油灯": 0 },
    //             { "灯": 9 },
    //         ],
    //         "窗户": [
    //             { "窗户": 0 },
    //             { "窗户": 3 },
    //         ],
    //         "窗帘": [
    //             { "窗帘": 0 },
    //             { "窗帘": 4 },
    //         ],
    //         "门": [
    //             { "门": 0 },
    //             { "门": 2 },
    //         ],
    //         "床": [
    //             { "床": 0 },
    //             { "床": 12 },
    //         ],
    //         "打扫": [
    //             { "桌子": 0 },
    //             { "扫地机": 10 },
    //         ],
    //         "衣服": [
    //             { "小男孩小女孩": 0 },
    //             { "衣服": 1 },
    //         ],
    //         "取暖": [
    //             { "柴火堆": 0 },
    //             { "电暖气": 7 },
    //         ],
    //         "娱乐": [
    //             { "收音机": 0 },
    //             { "电视": 8 },
    //         ],
    //         "桌子": [
    //             { "桌子": 0 },
    //             { "桌子": 11 },
    //         ]
    //     }


    static shengjidian =
        {
            "墙": [
                { "墙壁": 0 },
                { "墙壁": 20 },
            ],
            "地板": [
                { "地板": 0 },
                { "地板": 30 },
            ],
            "灯": [
                { "煤油灯": 0 },
                { "灯": 120 },
            ],
            "窗户": [
                { "窗户": 0 },
                { "窗户": 6 },
            ],
            "窗帘": [
                { "窗帘": 0 },
                { "窗帘": 12 },
            ],
            "门": [
                { "门": 0 },
                { "门": 3 },
            ],
            "床": [
                { "床": 0 },
                { "床": 350 },
            ],
            "打扫": [
                { "桌子": 0 },
                { "扫地机": 170 },
            ],
            "衣服": [
                { "小男孩小女孩": 0 },
                { "衣服": 1 },
            ],
            "取暖": [
                { "柴火堆": 0 },
                { "电暖气": 50 },
            ],
            "娱乐": [
                { "收音机": 0 },
                { "电视": 80 },
            ],
            "桌子": [
                { "桌子": 0 },
                { "桌子": 230 },
            ]
        }
}


